package com.ali.common;

import java.util.Arrays;
import java.util.Comparator;

public class ArrayUtil {
//Array

    /**
     * 获取数组长度
     */
    public static int getLength(Object[] os) {
        return os.length;
    }

    /**
     * 判断是否为同一数组
     */
    public static boolean equals(Object[] os1, Object[] os2) {
        return Arrays.equals(os1, os2);
    }

    /**
     * 排序
     */
    public static Object[] sort(Object[] os) {
        Arrays.sort(os);
        return os;
    }

    /**
     * 自定义排序 - 正序
     */
    public static Object[] customizeSortAsc(Object[] os) {
        Arrays.sort(os, new Comparator<>() {
            @Override
            public int compare(Object o1, Object o2) {
                String str1 = (String) o1;
                String str2 = (String) o2;
                return str1.compareTo(str2);
            }
        });
        return os;
    }

    /**
     * 自定义排序 - 倒序
     */
    public static Object[] customizeSortDesc(Object[] os) {
        Arrays.sort(os, new Comparator<>() {
            @Override
            public int compare(Object o1, Object o2) {
                String str1 = (String) o1;
                String str2 = (String) o2;
                return str2.compareTo(str1);
            }
        });
        return os;
    }

    /**
     * 数组拷贝
     */
    public static Object[] arraycopy(Object[] os) {
        Object[] newOs = new Object[os.length];
        System.arraycopy(os, 0, newOs, 0, os.length);
        return newOs;
    }
}